package com.springboot.product.coltroller;

import com.springboot.product.entities.Product;
import com.springboot.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class Controller {
    @Autowired
    ProductService productService;

    @GetMapping("/products")
    public List<Product> getAllProducts(){
        return productService.findAllProducts();
    }

    @GetMapping("/product/{id}")
    public ResponseEntity<Product> getProductById(@PathVariable("id") String ID ){
        Product product = productService.getProductById(ID);
        if(product == null){
            return new ResponseEntity<>(product, HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(product, HttpStatus.OK);
    }

    @GetMapping("/product/delete/{id}")
    public List<Product> deleteById(@PathVariable Long id){
        productService.deleteProductById(id);
        return productService.findAllProducts();
    }

    @PostMapping("/product/add")
    public Product addProduct(@RequestBody Product product){
       return productService.saveOrUpdateProduct(product);
    }
}
